﻿//------------------------------------------------------------------------------------------------------
// The MIT License (MIT) Copyright (c) 2014 Marton Rusko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------------------------------
#if !NETFX_CORE
#endif
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using Ninject;

namespace RM.Banking.Common
{
    /// <summary>
    /// IoC facade
    /// </summary>
    public static class IoC
    {
        private readonly static IKernel Kernel =
            new StandardKernel(new NinjectSettings()
            {
#if !WINDOWS_PHONE
                InjectNonPublic = true
#endif
            });


        public static void Inject(object o)
        {
            Kernel.Inject(o);
        }

        public static T Get<T>()
        {
            return Kernel.Get<T>();
        }

        public static IEnumerable<T> GetAll<T>()
        {
            return Kernel.GetAll<T>();
        }

        public static T Get<T>(string name)
        {
            return Kernel.Get<T>(name);
        }

        public static T Get<T>(IEnumerable<string> names) where T : class
        {
            foreach (var name in names)
            {
                try
                {
                    var result = Kernel.Get<T>(name);
                    return result;
                }
                catch (Exception ex)
                {
                    var bindings = Kernel.GetBindings(typeof(T));

                    //Debug.WriteLine(ex);
                    Debug.WriteLine(string.Format("Ninject failed: {0} ({1}): {2}", name, typeof(T), ex));
                    foreach (var binding in bindings)
                    {
                        Debug.WriteLine(binding.BindingConfiguration.Metadata.Name);
                    }
                }
            }
            return (T)null;
        }

        public static void RegisterModule<T>()
        {
            RegisterModule(typeof(T));
        }

        public static void RegisterModule(Type type)
        {
#if NETFX_CORE
            RegisterModule(type.GetTypeInfo().Assembly);
#else
            RegisterModule(type.Assembly);
#endif
        }

        public static void RegisterModule(Assembly assembly)
        {
            //TODO: Implment WP7 call
#if !WINDOWS_PHONE
            Kernel.Load((IEnumerable<Assembly>)new[] { assembly });
#endif
            //SetupAutomapper(assembly);
        }
    }

}
